package com.purchase.ml.resource.report;

import com.purchase.ml.annotations.OperLog;
import com.purchase.ml.bean.base.ApiRestResult;
import com.purchase.ml.bean.base.PageDto;
import com.purchase.ml.bean.page.Page;
import com.purchase.ml.bean.req.prod.SysCategoryNewDto;
import com.purchase.ml.bean.req.report.*;
import com.purchase.ml.bean.res.report.ReportAdminRes;
import com.purchase.ml.bean.res.report.ReportMasterDealerRes;
import com.purchase.ml.bean.res.report.vo.ReportDealerSalesVo;
import com.purchase.ml.consts.CommonConsts;
import com.purchase.ml.consts.PermissionsConst;
import com.purchase.ml.entity.acc.SysUser;
import com.purchase.ml.entity.dealer.SysDealer;
import com.purchase.ml.entity.order.SysDealerCollect;
import com.purchase.ml.enums.RetStatusNum;
import com.purchase.ml.mapper.dealer.SysDealerMapper;
import com.purchase.ml.resource.BaseResource;
import com.purchase.ml.security.JwtUtil;
import com.purchase.ml.service.dealer.SysDealerService;
import com.purchase.ml.service.report.ReportService;
import com.purchase.ml.utils.DateUtils;
import com.purchase.ml.utils.RegexpUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author kyyang
 * Created 2021-03-04
 */

@RestController
@RequestMapping("/report")
@Api(tags = "统计报告")
@Slf4j
public class ReportResource extends BaseResource {

    @Autowired
    private ReportService reportService;
    @Autowired
    private SysDealerMapper sysDealerMapper;

    @RequestMapping(method = RequestMethod.POST, path = "/admin")
    @ApiOperation("查询统计报告")
    @RequiresPermissions(PermissionsConst.REPORT_ADMIN_QUERY)
    public ApiRestResult adminReport(@RequestBody @Valid ReportAdminReq req) throws Exception {
        int dateDiff = DateUtils.getDateDiff(req.getStartTime(), req.getEndTime());
        if(CommonConsts.MAX_REPORT_DIFF_DAYS<dateDiff){
            log.error("当前时间范围天数为：[{}],超过上限 93 天",dateDiff);
            return ApiRestResult.err("Jarak masa yang dipilih：["+dateDiff+"],telah melebihi 93 hari","400");
        }
        ReportAdminRes result =  reportService.queryAdminReport(req);
        return ApiRestResult.ok(result);
    }


    @RequestMapping(method = RequestMethod.POST, path = "/masterdealer")
    @ApiOperation("主经销商查询统计报告")
    @RequiresPermissions(PermissionsConst.REPORT_MASTERDEALER_QUERY)
    public ApiRestResult masterDealerReport(@RequestBody @Valid ReportAdminReq req) throws Exception {
        int dateDiff = DateUtils.getDateDiff(req.getStartTime(), req.getEndTime());
        if(CommonConsts.MAX_REPORT_DIFF_DAYS<dateDiff){
            log.error("当前时间范围天数为：[{}],超过上限 93 天",dateDiff);
            return ApiRestResult.err("Jarak masa yang dipilih：["+dateDiff+"],telah melebihi 93 hari","400");
        }
        SysUser currentUser = getCurrentUser();
        if(currentUser.getRoleType()==CommonConsts.ROLETYPE_MASTER){
            req.setDealerId(currentUser.getDealerId());
        }
        //管理员 大代理 dealerId 必传
        if((currentUser.getRoleType()==CommonConsts.ROLETYPE_ADMIN||currentUser.getRoleType()==CommonConsts.ROLETYPE_AGENCY)&&req.getDealerId()==null){
            log.error("[{}],管理员或者大代理 dealerId 必传",currentUser.getAcc());
            return ApiRestResult.err("dealerId Admin atau Owner tidak boleh dikosongkan","400");
        }


        ReportMasterDealerRes result =  reportService.queryMasterDealerReport(req);
        return ApiRestResult.ok(result);
    }


    @RequestMapping(method = RequestMethod.POST, path = "/masterdealer/sales/page")
    @ApiOperation("主经销商|经销商 汇总数据分页查询")
    @RequiresPermissions(PermissionsConst.REPORT_MASTERDEALER_SALES_QUERY)
    public ApiRestResult masterDealerSalesReport(@RequestBody @Valid ReportDealerReq reportDealerReq) {

        int dateDiff = DateUtils.getDateDiff(reportDealerReq.getStartTime(), reportDealerReq.getEndTime());
        if(CommonConsts.MAX_REPORT_DIFF_DAYS<dateDiff){
            log.error("当前时间范围天数为：[{}],超过上限 93 天",dateDiff);
            return ApiRestResult.err("Jarak masa yang dipilih：["+dateDiff+"],telah melebihi 93 hari","400");
        }
        
        SysUser currentUser = getCurrentUser();
        if(currentUser.getRoleType()==CommonConsts.ROLETYPE_MASTER){
            reportDealerReq.setDealerId(currentUser.getDealerId());
        }
        //管理员 大代理 dealerId 必传
        /*if((currentUser.getRoleType()==CommonConsts.ROLETYPE_ADMIN||currentUser.getRoleType()==CommonConsts.ROLETYPE_AGENCY)&&reportDealerReq.getDealerId()==null){
            log.error("[{}],管理员或者大代理 dealerId 必传",currentUser.getAcc());
            return ApiRestResult.err("管理员或者大代理 dealerId 不能为空","400");
        }*/
        Page<ReportDealerSalesVo> result =  reportService.queryMasterDealerSalesPageReport(reportDealerReq);
        return ApiRestResult.ok(result);
    }


    @RequestMapping(method = RequestMethod.POST, path = "/online/transaction")
    @ApiOperation("线上交易报告")
    @RequiresPermissions(PermissionsConst.REPORT_ADMIN_MASTER_REPORT)
    public ApiRestResult onlineTransactionReport(@RequestBody @Valid ReportOnlineTransactionReq req) throws Exception {
        int dateDiff = DateUtils.getDateDiff(req.getStartTime(), req.getEndTime());
        if(CommonConsts.MAX_REPORT_ONLINE_DIFF_DAYS<dateDiff){
            log.error("当前时间范围天数为：[{}],超过上限 30 天",dateDiff);
            return ApiRestResult.err("Jarak masa yang dipilih：["+dateDiff+"],telah melebihi 30 hari","400");
        }
        Map<String,Object> result =  reportService.queryOnlineTransactionReport(req);
        return ApiRestResult.ok(result);
    }


    @RequestMapping(method = RequestMethod.POST, path = "/all_master_dealer/sales")
    @ApiOperation("所有主经销商销售报告|某主经销商下经销商报告")
    @RequiresPermissions(PermissionsConst.REPORT_ADMIN_MASTER_REPORT)
    public ApiRestResult masterDealerSalesReport(@RequestBody @Valid ReportMasterDearSaleReq req) throws Exception {
        int dateDiff = DateUtils.getDateDiff(req.getStartTime(), req.getEndTime());
        if(CommonConsts.MAX_REPORT_ONLINE_DIFF_DAYS < dateDiff){
            log.error("当前时间范围天数为：[{}],超过上限 30 天",dateDiff);
            return ApiRestResult.err("Jarak masa yang dipilih：["+dateDiff+"],telah melebihi 30 hari","400");
        }
        Map<String,Object> result =  reportService.queryMasterDealerSalesReport(req);
        return ApiRestResult.ok(result);
    }


    @RequestMapping(method = RequestMethod.POST, path = "/product/sales")
    @ApiOperation("产品销售报告")
     @RequiresPermissions(PermissionsConst.REPORT_ADMIN_MASTER_REPORT)
    public ApiRestResult productSalesReport(@RequestBody @Valid ReportProductSaleReq req) throws Exception {
        int dateDiff = DateUtils.getDateDiff(req.getStartTime(), req.getEndTime());
        if(CommonConsts.MAX_REPORT_ONLINE_DIFF_DAYS<dateDiff){
            log.error("当前时间范围天数为：[{}],超过上限 30 天",dateDiff);
            return ApiRestResult.err("Jarak masa yang dipilih：["+dateDiff+"],telah melebihi 30 hari","400");
        }
        Map<String,Object> result =  reportService.queryProductSalesReport(req);
        return ApiRestResult.ok(result);
    }


}